#include "Arduino.h"
#include "RF433send.h"
#include "OneButton.h"
#include "recv.h"

#define BTN_PIN 9
#define PIN_RFOUT 3 // send pin

OneButton btn(BTN_PIN, true, true);
RfSend *tx;

void on_click()
{
  byte data[] = {0x4e, 0xe1, 0x01};
  tx->send(sizeof(data), data);

  Serial.println("Send unlock.");
}

void on_double_click()
{
  byte data[] = {0x4e, 0xe1, 0x02};
  tx->send(sizeof(data), data);

  Serial.println("Send lock.");
}

void on_long_press_start()
{
  byte data[] = {0x4e, 0xe1, 0x04};
  tx->send(sizeof(data), data);

  Serial.println("Send Power On");
}

void setup()
{
  Serial.begin(9600);
  pinMode(PIN_RFOUT, OUTPUT);

  setup_recv();

  btn.attachClick(on_click);
  btn.attachDoubleClick(on_double_click);
  btn.attachLongPressStart(on_long_press_start);

  tx = rfsend_builder(
      RfSendEncoding::MANCHESTER,
      PIN_RFOUT,
      RFSEND_DEFAULT_CONVENTION, // Do we want to invert 0 and 1 bits? No.
      4,                         // Number of sendings
      nullptr,                   // No callback to keep/stop sending (if you want to send
                                 // SO LONG AS a button is pressed, the function reading the
                                 // button state is to be put here).
      12000,                     // initseq
      0,                         // lo_prefix
      0,                         // hi_prefix
      0,                         // first_lo_ign
      400,                       // lo_short
      1200,                      // lo_long
      0,                         // hi_short
      0,                         // hi_long
      400,                       // lo_last
      12000,                     // sep
      24                         // nb_bits
  );
}

void loop()
{
  btn.tick();
  tick_recv();
}
